from django.shortcuts import render,HttpResponse

# Create your views here.

def login(request):
    if request.method == 'GET': # 表示浏览器端请求数据，我们就返回html文件
        # print(request.method, type(request.method)) # GET <class 'str'> POST <class 'str'>
        # return render(request,'login.html')
        # print(request.GET)  # <QueryDict: {'username': ['nana'], 'password': ['1233'], 'hobby': ['1', '2', '3']}>
        # print(request.GET.get('username')) # nana
        # print(request.GET.getlist('hobby')) # ['1', '2', '3']
        return render(request, 'login.html')
    # elif request.method == 'POST':  # 表示浏览器端提交form表单的数据
    #         # 处理post请求相关的数据
    #         print('post')
    #         return HttpResponse("post")

    if request.method == 'POST':
        # print(request.POST)   # <<QueryDict: {'username': ['nana'], 'password': ['1123'], 'hobby': ['1', '2']}>
        # print(request.POST.get('username')) # nana
        # print(request.POST.get('password')) # 1123
        # print(request.POST.get('hobby'))  # 2
        # print(request.POST.getlist('hobby'))  # ['1', '2']
        # return HttpResponse("post")

        from app01 import models
        # 密码比对
        # 1.获取提交过来的用户名密码
        username = request.POST.get('username')
        password =request.POST.get('password')
        # 2.拿着提交过来的数据跟数据库里面的用户名和密码比对
        # res1 = models.UserInfo.objects
        # # print(res1)  # app01.UserInfo.objects
        # res = models.UserInfo.objects.filter(username=username,password=password)  # select * from userinfo where username = '' and password = ''
        # print(res) # <QuerySet [<UserInfo: nana>]>
        # print(res[0].username)  # nana
        # print(res[0].password)  # 123

        res = models.UserInfo.objects.filter(username=username,password=password).first()
        if res:
            print('登陆成功')
            models.UserInfo.objects.create(username='tank', password='123',age=18)
            print('创建成功')
        else:
            print('用户名或密码错误')
        return HttpResponse("post")








def do_login(request):

    print('do_login')
    return HttpResponse('do_login')